<?php

namespace app\admin\model;

/**
 * 书库-部分素材模型
 */
class AdsBookMaterial extends \think\Model{

	/**
	 * 从原素材表中 将平台类型转成这边的 分类类型
	 *
	 * @Author 小邓
	 * @DateTime 2022-11-17
	 * @param Type|null $var
	 * @return void
	 */
	public function typeAndPlatformIdsToCateType($platform_id, $type) {
		if($platform_id == 1 && $type == 1){
			return 'baidu_text';
		}

		if($platform_id == 2 && $type == 1){
			return 'zhihu_text';
		}

		if($platform_id == 3 && $type == 1){
			return 'douyin_text';
		}

		if($platform_id == 4 && $type == 1){
			return 'weibo_text';
		}

		if($platform_id == 5 && $type == 1){
			return 'tencent_text';
		}

		if($type == 6){
			return 'img';
		}

		if($type == 3){
			return 'video';
		}

		if($type == 13){
			return 'bgm';
		}

		return '';
	}

	/**
	 * 将素材库过稿后的数据保存起来
	 *
	 * @Author 小邓
	 * @DateTime 2022-11-17
	 * @param [type] $materialMy
	 * @param [type] $uid
	 * @return void
	 */
	public function saveBookMaterial($materialMy, $uid=null){
		$platform_ids = $materialMy['platform_ids'];
		if(stristr($platform_ids, ",")){
			$platform_ids = explode(",", $platform_ids);
		}else{
			$platform_ids = [$platform_ids];
		}
		$insertData = [];
		foreach ($platform_ids as $platform_id) {
			$cate_type = $this->typeAndPlatformIdsToCateType($platform_id, $materialMy['type']);
			if(!$cate_type){
				continue;
			}
			$insertData[] = [
				'uid' => $uid ?: $materialMy['uid'],
				'ads_material_my_id' => $materialMy['id'],
				'create_uid'  => $materialMy['uid'], //创建人
				'content'     => $materialMy['content'],
				'title'       => $materialMy['title'],
				'cover'       => $materialMy['cover'],
				'cate_type'   => $cate_type,
				'book_id'     => $materialMy['book_id'],
				'update_time' => time(),
				'create_time' => time(),
			];
		}
		db("ads_book_material")->insertAll($insertData);
		return true;
	}
}